package org.eztouch.service;

import java.util.List;

import org.eztouch.sys.entity.SysMenu;
import org.eztouch.sys.facade.SysMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;

import feign.hystrix.FallbackFactory;


/**
 * Spring Cloud而言Ribbon或Feign这个是微服务内部调用框架，无法调用zuul网关的地址，
 * 如需通过网关调用，则使用httpClient来调用。
 */
@FeignClient(name="sys-service",fallbackFactory=SysMenuClinet.SysMenuServiceFallBackFactory.class)
public interface SysMenuClinet extends SysMenuService{
	
	/**
	 * Feign Hystrix回退<br>
	 * Hystrix 能使你的系统在出现依赖服务失效的时候，通过隔离系统所依赖的服务，
	 * 防止服务级联失败，同时提供失败回退机制，更优雅地应对失效
	 */
	@Component
	class SysMenuServiceFallBackFactory implements FallbackFactory<SysMenuClinet>{

		@Override
		public SysMenuClinet create(final Throwable e) {
			return new SysMenuClinet(){
				private final Logger log = LoggerFactory.getLogger(SysMenuService.class);

				@Override
				public SysMenu insert(SysMenu entity) {
					log.info("服务调用异常，进入Hystrix回退逻辑！",e);
					return null;
				}

				@Override
				public boolean deleteById(String id) {
					log.info("服务调用异常，进入Hystrix回退逻辑！",e);
					return false;
				}

				@Override
				public boolean flagDeleteById(String id) {
					log.info("服务调用异常，进入Hystrix回退逻辑！",e);
					return false;
				}

				@Override
				public SysMenu updateById(SysMenu entity) {
					log.info("服务调用异常，进入Hystrix回退逻辑！",e);
					return null;
				}

				@Override
				public SysMenu selectById(String id) {
					log.info("服务调用异常，进入Hystrix回退逻辑！",e);
					return null;
				}

				@Override
				public List listByRoleIds(String roleIds) {
					log.info("服务调用异常，进入Hystrix回退逻辑！",e);
					return null;
				}

			};
		}
		
	}

}
